package com.cndongtai.net.analysis;

import java.util.Date;

import com.cndongtai.constant.LightConstant;
import com.cndongtai.net.NetController;
import com.cndongtai.net.SocketContainer;
import com.cndongtai.net.message.Message;
import com.cndongtai.net.redis.RedisServer;
import com.cndongtai.util.ByteUtil;

public class MessageFilter {

	public void doMsg(byte[] msg) {
		// 判断报文类型
		String type = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[4])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[5])).toUpperCase());
		if("28B1".equals(type)) {
			// 读取DTU状态
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
				
				String dtuStatus = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase());
				
				int temperature = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[18])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[19])).toUpperCase()),16)-400;
				
				int vol = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[20])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[21])).toUpperCase()),16);
				System.out.println("temperature=>" +(temperature/10.0) + "  vol=>" +vol/10.0  + "  dtuStatus=>" +dtuStatus);
				
				rsp.setValue("dtuStatus", dtuStatus);
				rsp.setValue("temperature", temperature+"");
				rsp.setValue("vol", vol + "");
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("28B2".equals(type)) {
			// 上报所管的RTU
			// 要求DTU上报RTU编号
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			
			System.out.println("rsp SEQ=>"+seq);
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
				
				int lightSize = ByteUtil.getUnsignedByte(msg[14]);
				rsp.setValue("lightSize", lightSize + "");
				if(lightSize>0) {
					for(int i=0;i<lightSize;i++) {
						System.out.println("=>"+ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15+i*2])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[16+i*2])).toUpperCase()));
						rsp.setValue("lightNum"+i, ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15+i*2])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[16+i*2])).toUpperCase()));
					}
				}
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("28B3".equals(type)) {
			// 配置RTU
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			System.out.println("rsp res>"+res);
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
				
				System.out.println("config rtu success");
				
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", res);
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("28B5".equals(type)) {
			// 读取RTU配置数据
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
				
				// RTU型号
				String lightType = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[16])).toUpperCase());
				System.out.println("lightType>" + lightType);
				rsp.setValue("lightType", lightType);
				
				// 序列号
				String serialNum = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[17])).toUpperCase()) 
						+ ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[18])).toUpperCase())
						+ ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[19])).toUpperCase())
						+ ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[20])).toUpperCase());
				System.out.println("serialNum>" + serialNum);
				rsp.setValue("serialNum", serialNum);
				
				// 生产时间
				StringBuffer productTime = new StringBuffer();
				productTime.append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[21])).toUpperCase()))
						   .append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[22])).toUpperCase()))
						   .append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[23])).toUpperCase()))
						   .append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[25])).toUpperCase()))
						   .append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[26])).toUpperCase()))
						   .append(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[27])).toUpperCase()));
				System.out.println("productTime>"+productTime);
				rsp.setValue("productTime", productTime.toString());
				
				// 控制模式
				String controlModel = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[28])).toUpperCase());
				System.out.println("controlModel>" + controlModel);
				rsp.setValue("controlModel", controlModel);
				
				// 开灯电压
				int turnonVol = Integer.valueOf(Integer.toHexString(ByteUtil.getUnsignedByte(msg[29])).toUpperCase(),16);
				System.out.println("turnonVol>"+turnonVol);
				rsp.setValue("turnonVol", turnonVol+"");
				
				// 开灯时间
				String turnonTime = ByteUtil.getUnsignedByte(msg[30]) + ":" + ByteUtil.getUnsignedByte(msg[31]);
				System.out.println("turnonTime>"+turnonTime);
				rsp.setValue("turnonTime", turnonTime);
				
				// 关灯时间
				String shutdownTime = ByteUtil.getUnsignedByte(msg[32]) + ":" + ByteUtil.getUnsignedByte(msg[33]);
				System.out.println("shutdownTime>"+shutdownTime);
				rsp.setValue("shutdownTime", shutdownTime);
				
				// 工作模式
				String workModel = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[34])).toUpperCase());
				System.out.println("workModel>"+workModel);
				rsp.setValue("workModel", workModel);
				
				// 峰值保护
				int topVol = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[35])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[36])).toUpperCase()),16);
				System.out.println("topVol>"+topVol);
				rsp.setValue("topVol", topVol+"");
				
				// 欠压保护
				int lowVol = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[37])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[38])).toUpperCase()),16);
				System.out.println("lowVol>"+lowVol);
				rsp.setValue("lowVol", lowVol+"");
				
				// 退出保护
				int exitVol = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[39])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[40])).toUpperCase()),16);
				System.out.println("exitVol>"+exitVol);
				rsp.setValue("exitVol", exitVol+"");
				
				// 降功率起始电压
				int lowStartVol = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[41])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[42])).toUpperCase()),16);
				System.out.println("lowStartVol>"+lowStartVol);
				rsp.setValue("lowStartVol", lowStartVol+"");
				
				// 降功率终止电压
				int lowEndVol = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[43])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[44])).toUpperCase()),16);
				System.out.println("lowVol>"+lowEndVol);
				rsp.setValue("lowEndVol", lowEndVol+"");
				
				// 1段时长
				int time1 = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[45])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[46])).toUpperCase()),16);
				// 1段电流
				int workCurrent1 = ByteUtil.getUnsignedByte(msg[47]);
				System.out.println("time1>" + time1 + " workCurrent1>"+workCurrent1);
				rsp.setValue("time1", time1+"");
				rsp.setValue("workCurrent1", workCurrent1+"");
				
				// 2段时长
				int time2 = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[48])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[49])).toUpperCase()),16);
				// 2段电流
				int workCurrent2 = ByteUtil.getUnsignedByte(msg[50]);
				System.out.println("time2>" + time2 + " workCurrent2>"+workCurrent2);
				rsp.setValue("time2", time2+"");
				rsp.setValue("workCurrent2", workCurrent2+"");
				
				// 3段时长
				int time3 = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[51])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[52])).toUpperCase()),16);
				// 3段电流
				int workCurrent3 = ByteUtil.getUnsignedByte(msg[53]);
				System.out.println("time3>" + time3 + " workCurrent3>"+workCurrent3);
				rsp.setValue("time3", time3+"");
				rsp.setValue("workCurrent3", workCurrent3+"");
				
				// 4段时长
				int time4 = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[54])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[55])).toUpperCase()),16);
				// 4段电流
				int workCurrent4 = ByteUtil.getUnsignedByte(msg[56]);
				System.out.println("time4>" + time4 + " workCurrent4>"+workCurrent4);
				rsp.setValue("time4", time4+"");
				rsp.setValue("workCurrent4", workCurrent4+"");
				
				// 负载最大电流
				int maxWorkCurrent = Integer.valueOf(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[58])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[59])).toUpperCase()),16);
				System.out.println(" maxWorkCurrent>"+maxWorkCurrent);
				rsp.setValue("maxWorkCurrent", maxWorkCurrent+"");
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("28B6".equals(type)) {
			// 读取RTU状态信息
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String panId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String lightNum = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			String code = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			System.out.println("code:" + code);
			if("0000".equals(code)) {
				rsp.setValue("code", "00");
			}else {
				rsp.setValue("code", "11");
			}
			
			String statusCode = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[16])).toUpperCase());
			
			rsp.setValue("panId", panId);
			rsp.setValue("lightNum", lightNum);
			rsp.setValue("statusCode", statusCode);
			
			System.out.println("panId:"+panId+"    lightNum:"+lightNum);
			System.out.println("statusCode:"+statusCode);
			
			int temperature = (ByteUtil.getUnsignedByte(msg[20])*256 + ByteUtil.getUnsignedByte(msg[21]))-400;
			System.out.println("temperature>"+temperature);
			rsp.setValue("temperature", temperature+"");
			
			int solarPanelVol = (ByteUtil.getUnsignedByte(msg[22])*256 + ByteUtil.getUnsignedByte(msg[23]));
			System.out.println("solarPanelVol>"+solarPanelVol);
			rsp.setValue("solarPanelVol", solarPanelVol+"");
			
			int batteryVol = (ByteUtil.getUnsignedByte(msg[24])*256 + ByteUtil.getUnsignedByte(msg[25]));
			System.out.println("batteryVol>"+batteryVol);
			rsp.setValue("batteryVol", batteryVol+"");
			
			Integer chargeCurrent = ByteUtil.getUnsignedByte(msg[26])*256 + ByteUtil.getUnsignedByte(msg[27]);
			System.out.println("chargeCurrent>"+chargeCurrent);
			rsp.setValue("chargeCurrent", chargeCurrent+"");
			
			Integer workCurrent = ByteUtil.getUnsignedByte(msg[28])*256 + ByteUtil.getUnsignedByte(msg[29]);
			System.out.println("workCurrent>"+workCurrent);
			rsp.setValue("workCurrent", workCurrent+"");
			
			int workVol = (ByteUtil.getUnsignedByte(msg[30])*256 + ByteUtil.getUnsignedByte(msg[31]));
			System.out.println("workVol>"+workVol);
			rsp.setValue("workVol", workVol+"");
			
			int chargeCount = (ByteUtil.getUnsignedByte(msg[32])*256*256*256 + ByteUtil.getUnsignedByte(msg[33])*256*256 + ByteUtil.getUnsignedByte(msg[34])*256 + ByteUtil.getUnsignedByte(msg[35]));
			System.out.println("chargeCount>"+chargeCount);
			rsp.setValue("chargeCount", chargeCount+"");
			
			int workCount = (ByteUtil.getUnsignedByte(msg[36])*256*256*256 + ByteUtil.getUnsignedByte(msg[37])*256*256 + ByteUtil.getUnsignedByte(msg[38])*256 + ByteUtil.getUnsignedByte(msg[39]));
			System.out.println("workCount>"+workCount);
			rsp.setValue("workCount", workCount+"");
			
			NetController.setMessage(seq, rsp);
		}else if("38B7".equals(type)) {
			// RTU主动上报电流
			Message message = new Message();
			
			String panId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String lightNum = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			String currentType = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase());
			message.setType(type);
			message.setValue("panId", panId);
			message.setValue("lightNum", lightNum);
			message.setValue("currentType", currentType);
			
			System.out.println("panId:"+panId+"    lightNum:"+lightNum);
			if("00".equals(currentType)) {
				System.out.println("currentType:充电");
			}else {
				System.out.println("currentType:放电");
			}
			
			Integer num = ByteUtil.getUnsignedByte(msg[15]);
			message.setValue("num", num+"");
			System.out.println("num:" + num);
			for(int i=0;i<num;i++) {
				Integer workCurrent = ByteUtil.getUnsignedByte(msg[16+i*2])*256 + ByteUtil.getUnsignedByte(msg[17+i*2]);
				System.out.println("workCurrent"+i + ":"+ workCurrent);
				message.setValue("workCurrent"+i, workCurrent+"");
			}
			
			String collectTime = ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[16+num*2]) + "")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[17+num*2]) + "")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[18+num*2]) + "")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[20+num*2]) + "")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[21+num*2]) + "")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[22+num*2]) + "");
			
			message.setValue("collectTime", collectTime);
			System.out.println("collectTime:"+collectTime);
			
			NetController.putMsg(message);
			
			// 组织响应报文
			byte[] res = new byte[20];
			res[0] = msg[0];
			res[1] = msg[1];
			
			// 长度
			res[2] = Long.valueOf(0x00).byteValue();
			res[3] = Long.valueOf(0x14).byteValue();
			
			// 类型
			res[4] = msg[4];
			res[5] = msg[5];
			
			// 信息序号
			res[6] = msg[6];
			res[7] = msg[7];
			
			// PanId
			res[8] = msg[8];
			res[9] = msg[9];
			
			// DTU的deviceId
			res[10] = msg[10];
			res[11] = msg[11];
			
			// RTU的deviceId
			res[12] = msg[12];
			res[13] = msg[13];
			
			// 结果码
			res[14] = Long.valueOf(0x00).byteValue();
			res[15] = Long.valueOf(0x00).byteValue();
			
			// CRC16
			int crcval = ByteUtil.cal_crc(res, 16);
			res[16] = Long.valueOf(crcval/256).byteValue();
			res[17] = Long.valueOf(crcval%256).byteValue();
			
			res[18] = Long.valueOf(0x00).byteValue();
			res[19] = Long.valueOf(0x00).byteValue();
			
			SocketContainer container = NetController.getSocketPool().get(panId);
			if(container!=null) {
				container.send(res);
			}
			
		}else if("28B8".equals(type)) {
			// 开关灯命令
			Message rsp = new Message();
			
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("38B9".equals(type)) {
			// RTU主动上报状态
			Message message = new Message();
			
			String panId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String lightNum = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			String statusCode = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase());
			String lightCode = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[16])).toUpperCase());
			message.setType(type);
			message.setValue("panId", panId);
			message.setValue("lightNum", lightNum);
			message.setValue("statusCode", statusCode);
			message.setValue("lightCode", lightCode);
			
			System.out.println("panId:"+panId+"    lightNum:"+lightNum);
			System.out.println("statusCode:"+statusCode);
			System.out.println("lightCode:"+lightCode);
			
			Integer temperature = (ByteUtil.getUnsignedByte(msg[18])*256 + ByteUtil.getUnsignedByte(msg[19]))-400;
			message.setValue("temperature", temperature+"");
			System.out.println("temperature>"+temperature);
			
			Integer solarPanelVol = ByteUtil.getUnsignedByte(msg[20])*256 + ByteUtil.getUnsignedByte(msg[21]);
			message.setValue("solarPanelVol", solarPanelVol+"");
			System.out.println("solarPanelVol>"+solarPanelVol);
			
			Integer batteryVol = (ByteUtil.getUnsignedByte(msg[22])*256 + ByteUtil.getUnsignedByte(msg[23]));
			message.setValue("batteryVol", batteryVol+"");
			System.out.println("batteryVol>"+batteryVol);
			
			Integer chargeCurrent = ByteUtil.getUnsignedByte(msg[24])*256 + ByteUtil.getUnsignedByte(msg[25]);
			message.setValue("chargeCurrent", chargeCurrent+"");
			System.out.println("chargeCurrent>"+chargeCurrent);
			
			Integer workCurrent = ByteUtil.getUnsignedByte(msg[26])*256 + ByteUtil.getUnsignedByte(msg[27]);
			message.setValue("workCurrent", workCurrent+"");
			System.out.println("workCurrent>"+workCurrent);
			
			Integer workVol = ByteUtil.getUnsignedByte(msg[28])*256 + ByteUtil.getUnsignedByte(msg[29]);
			message.setValue("workVol", workVol+"");
			System.out.println("workVol>"+workVol);
			
			Integer chargeCount = ByteUtil.getUnsignedByte(msg[30])*256*256*256 + ByteUtil.getUnsignedByte(msg[31])*256*256 + ByteUtil.getUnsignedByte(msg[32])*256 + ByteUtil.getUnsignedByte(msg[33]);
			message.setValue("chargeCount", chargeCount+"");
			System.out.println("chargeCount>"+chargeCount);
			
			Integer workCount = ByteUtil.getUnsignedByte(msg[34])*256*256*256 + ByteUtil.getUnsignedByte(msg[35])*256*256 + ByteUtil.getUnsignedByte(msg[36])*256 + ByteUtil.getUnsignedByte(msg[37]);
			message.setValue("workCount", workCount+"");
			System.out.println("workCount>"+workCount);
			
			String collectTime = ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[38])+"")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[39])+"")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[40])+"")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[42])+"")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[43])+"")
					+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[44])+"");
			
			message.setValue("collectTime", collectTime);
			System.out.println("collectTime:"+collectTime);
			
			System.out.println("收到上报的状态信息");
			NetController.putMsg(message);
			System.out.println("上报的状态信息成功放入阻塞队列");
			
			// 组织响应报文
			byte[] res = new byte[20];
			res[0] = msg[0];
			res[1] = msg[1];
			
			// 长度
			res[2] = Long.valueOf(0x00).byteValue();
			res[3] = Long.valueOf(0x0E).byteValue();
			
			// 类型
			res[4] = Long.valueOf(0x38).byteValue();
			res[5] = Long.valueOf(0xA9).byteValue();
			
			// 信息序号
			res[6] = msg[6];
			res[7] = msg[7];
			
			// PanId
			res[8] = msg[8];
			res[9] = msg[9];
			
			// DTU的deviceId
			res[10] = msg[10];
			res[11] = msg[11];
			
			// RTU的deviceId
			res[12] = msg[12];
			res[13] = msg[13];
			
			// 结果码
			res[14] = Long.valueOf(0x00).byteValue();
			res[15] = Long.valueOf(0x00).byteValue();
			
			// CRC16
			int crcval = ByteUtil.cal_crc(res, 16);
			res[16] = Long.valueOf(crcval/256).byteValue();
			res[17] = Long.valueOf(crcval%256).byteValue();
			
			res[18] = Long.valueOf(0x82).byteValue();
			res[19] = Long.valueOf(0xB5).byteValue();
			
			SocketContainer container = NetController.getSocketPool().get(panId);
			if(container==null) {
				container = NetController.getSocketPool().get(panId+"_"+lightNum);
			}
			
			if(container!=null) {
				container.send(res);
			}
			
		}else if("28BA".equals(type)) {
			// 广播教时
			Message rsp = new Message();
			
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("28BB".equals(type)) {
			// 修改ID
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			if("0000".equals(res)) {
				rsp.setSeq(seq);
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
				
				String oldPanId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
				String oldDtuDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[10])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[11])).toUpperCase());
				String oldLightDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
				
				System.out.println("oldPanId==>"+oldPanId);
				System.out.println("oldDtuDeviceId==>"+oldDtuDeviceId);
				System.out.println("oldLightDeviceId==>"+oldLightDeviceId);
				
				rsp.setValue("oldPanId", oldPanId);
				rsp.setValue("oldDtuDeviceId", oldDtuDeviceId);
				rsp.setValue("oldLightDeviceId", oldLightDeviceId);
			}else {
				rsp.setSeq(seq);
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("80B1".equals(type)) {
			// 发起固件升级响应报文
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String oldPanId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String oldDtuDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[10])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[11])).toUpperCase());
			String oldLightDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			
			System.out.println("oldPanId==>"+oldPanId);
			System.out.println("oldDtuDeviceId==>"+oldDtuDeviceId);
			System.out.println("oldLightDeviceId==>"+oldLightDeviceId);
			rsp.setValue("oldPanId", oldPanId);
			rsp.setValue("oldDtuDeviceId", oldDtuDeviceId);
			rsp.setValue("oldLightDeviceId", oldLightDeviceId);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			if("0000".equals(res)) {
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
			}else {
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("80B0".equals(type)) {
			// 查询固件信息响应报文
			Message rsp = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			rsp.setSeq(seq);
			rsp.setType(type);
			
			String oldPanId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String oldDtuDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[10])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[11])).toUpperCase());
			String oldLightDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			
			System.out.println("oldPanId==>"+oldPanId);
			System.out.println("oldDtuDeviceId==>"+oldDtuDeviceId);
			System.out.println("oldLightDeviceId==>"+oldLightDeviceId);
			rsp.setValue("oldPanId", oldPanId);
			rsp.setValue("oldDtuDeviceId", oldDtuDeviceId);
			rsp.setValue("oldLightDeviceId", oldLightDeviceId);
			
			String res = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			System.out.println("seq==>"+seq);
			System.out.println("code==>"+res);
			
			if("0000".equals(res)) {
				rsp.setValue("code", "00");
				rsp.setValue("result","success");
				rsp.setValue("msg","成功");
				
				String protocalVersion = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[16])).toUpperCase());
				String hardwareVersion = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[17])).toUpperCase());
				String firmwareVersion = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[18])).toUpperCase());
				
				rsp.setValue("protocalVersion", protocalVersion);
				rsp.setValue("hardwareVersion", hardwareVersion);
				rsp.setValue("firmwareVersion", firmwareVersion);
				
				System.out.println("protocalVersion==>"+protocalVersion);
				System.out.println("hardwareVersion==>"+hardwareVersion);
				System.out.println("firmwareVersion==>"+firmwareVersion);
				
				String firmwareTime = ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[19])+"")
						+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[20])+"")
						+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[21])+"")
						+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[22])+"")
						+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[23])+"")
						+ ByteUtil.hexStringFormat(ByteUtil.getUnsignedByte(msg[24])+"");
				rsp.setValue("firmwareTime", firmwareTime);
				System.out.println("firmwareTime==>"+firmwareTime);
				
				StringBuffer imei = new StringBuffer();
				imei.append((char)ByteUtil.getUnsignedByte(msg[25]))
				    .append((char)ByteUtil.getUnsignedByte(msg[26]))
				    .append((char)ByteUtil.getUnsignedByte(msg[27]))
				    .append((char)ByteUtil.getUnsignedByte(msg[28]))
				    .append((char)ByteUtil.getUnsignedByte(msg[29]))
				    .append((char)ByteUtil.getUnsignedByte(msg[30]))
				    .append((char)ByteUtil.getUnsignedByte(msg[31]))
				    .append((char)ByteUtil.getUnsignedByte(msg[32]))
				    .append((char)ByteUtil.getUnsignedByte(msg[33]))
				    .append((char)ByteUtil.getUnsignedByte(msg[34]))
				    .append((char)ByteUtil.getUnsignedByte(msg[35]))
				    .append((char)ByteUtil.getUnsignedByte(msg[36]))
				    .append((char)ByteUtil.getUnsignedByte(msg[37]))
				    .append((char)ByteUtil.getUnsignedByte(msg[38]))
				    .append((char)ByteUtil.getUnsignedByte(msg[39]));
				
				rsp.setValue("imei", imei.toString());
				System.out.println("imei==>"+imei.toString());
				
				StringBuffer iccid = new StringBuffer();
				
				iccid.append((char)ByteUtil.getUnsignedByte(msg[40]))
					 .append((char)ByteUtil.getUnsignedByte(msg[41]))
					 .append((char)ByteUtil.getUnsignedByte(msg[42]))
					 .append((char)ByteUtil.getUnsignedByte(msg[43]))
					 .append((char)ByteUtil.getUnsignedByte(msg[44]))
					 .append((char)ByteUtil.getUnsignedByte(msg[45]))
					 .append((char)ByteUtil.getUnsignedByte(msg[46]))
					 .append((char)ByteUtil.getUnsignedByte(msg[47]))
					 .append((char)ByteUtil.getUnsignedByte(msg[48]))
					 .append((char)ByteUtil.getUnsignedByte(msg[49]))
					 .append((char)ByteUtil.getUnsignedByte(msg[50]))
					 .append((char)ByteUtil.getUnsignedByte(msg[51]))
					 .append((char)ByteUtil.getUnsignedByte(msg[52]))
					 .append((char)ByteUtil.getUnsignedByte(msg[53]))
					 .append((char)ByteUtil.getUnsignedByte(msg[54]))
					 .append((char)ByteUtil.getUnsignedByte(msg[55]))
					 .append((char)ByteUtil.getUnsignedByte(msg[56]))
					 .append((char)ByteUtil.getUnsignedByte(msg[57]))
					 .append((char)ByteUtil.getUnsignedByte(msg[58]))
					 .append((char)ByteUtil.getUnsignedByte(msg[59]));
				
				rsp.setValue("iccid", iccid.toString());
				System.out.println("iccid==>"+iccid.toString());
			}else {
				rsp.setValue("code", "11");
				rsp.setValue("result",res);
				rsp.setValue("msg", "失败");
			}
			
			NetController.setMessage(seq, rsp);
		}else if("80A2".equals(type)) {
			// 设备上报升级结果报文
			Message message = new Message();
			String seq = "" + Integer.parseInt(ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[6])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[7])).toUpperCase()), 16);
			message.setSeq(seq);
			message.setType(type);
			
			String panId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String dtuDeviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[10])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[11])).toUpperCase());
			String lightNum = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[12])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[13])).toUpperCase());
			
			System.out.println("panId==>"+panId);
			System.out.println("dtuDeviceId==>"+dtuDeviceId);
			System.out.println("lightNum==>"+lightNum);
			message.setValue("panId", panId);
			message.setValue("dtuDeviceId", dtuDeviceId);
			message.setValue("lightNum", lightNum);
			
			String rescode = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[14])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[15])).toUpperCase());
			
			message.setValue("code", rescode);
			System.out.println("返回结果==》"+rescode);
			
			System.out.println("收到上报的状态信息");
			NetController.putMsg(message);
			System.out.println("上报的状态信息成功放入阻塞队列");
			
			
			// 组织响应报文
			byte[] res = new byte[20];
			res[0] = msg[0];
			res[1] = msg[1];
			
			// 长度
			res[2] = Long.valueOf(0x00).byteValue();
			res[3] = Long.valueOf(0x0E).byteValue();
			
			// 类型
			res[4] = Long.valueOf(0x80).byteValue();
			res[5] = Long.valueOf(0xB2).byteValue();
			
			// 信息序号
			res[6] = msg[6];
			res[7] = msg[7];
			
			// PanId
			res[8] = msg[8];
			res[9] = msg[9];
			
			// DTU的deviceId
			res[10] = msg[10];
			res[11] = msg[11];
			
			// RTU的deviceId
			res[12] = msg[12];
			res[13] = msg[13];
			
			// 结果码
			res[14] = Long.valueOf(0x00).byteValue();
			res[15] = Long.valueOf(0x00).byteValue();
			
			// CRC16
			int crcval = ByteUtil.cal_crc(res, 16);
			res[16] = Long.valueOf(crcval/256).byteValue();
			res[17] = Long.valueOf(crcval%256).byteValue();
			
			res[18] = Long.valueOf(0x82).byteValue();
			res[19] = Long.valueOf(0xB5).byteValue();
			
			SocketContainer container = NetController.getSocketPool().get(panId);
			if(container==null) {
				container = NetController.getSocketPool().get(panId+"_"+lightNum);
			}
			
			if(container!=null) {
				container.send(res);
			}
		}else if("28AB".equals(type)) {
			// PC查询DTU中存储的RTU状态数据
			
		}else if("28AC".equals(type)) {
			// PC查询DTU中存储的RTU的电量数据
			
		}else if("48A2".equals(type)) {
			// 心跳包
			String panId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			SocketContainer container = NetController.getSocketPool().get(panId);
			if(container!=null) {
				container.send(msg);
				NetController.onlineMap.put(panId, System.currentTimeMillis());
			}
			System.out.println(new Date().toString()+" heart beat==>" + panId);
		}else if("48A5".equals(type)) {
			// 心跳包
			String panId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[8])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[9])).toUpperCase());
			String deviceId = ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[10])).toUpperCase()) + ByteUtil.hexStringFormat(Integer.toHexString(ByteUtil.getUnsignedByte(msg[11])).toUpperCase());
			panId += "_" + deviceId;
			SocketContainer container = NetController.getSocketPool().get(panId);
			if(container!=null) {
				container.send(msg);
				NetController.onlineMap.put(panId, System.currentTimeMillis());
			}
			
			// 心跳改变在线状态
			RedisServer.getInstance().set(LightConstant.REDIS_LIGHT_KEY+panId, "1", LightConstant.LIGHT_OFFLINE_TIMEOUT);
			System.out.println(new Date().toString()+" heart beat==>" + panId);
		}else {
			System.out.println("Error Msg Type===>" + type);
		}
		
		//NetController.setMessage(seq, rsp);
	}
	
}
